import { defineStore } from 'pinia'
import { ref, type Ref } from 'vue'
import type { UserInfo } from '@/types/user'
export const useLoginStore = defineStore(
  'login',
  () => {
    const token = ref('')
    const isFirstInit = ref(true)
    const isCollasped = ref(false)
    const userInfo: Ref<UserInfo> = ref({
      _id: '',
      username: '',
      password: '',
      gender: 0,
      introduction: '',
      avatar: '',
      token: '',
      role: undefined,
      createTime: new Date(),
      updateTime: new Date(),
    })

    function setToken(value: string) {
      token.value = value
    }
    function setIsFirstInit(value: boolean) {
      isFirstInit.value = value
    }
    function setCollasped(value: boolean) {
      isCollasped.value = value
    }

    function setUserInfo(value: UserInfo) {
      userInfo.value = value
    }

    //清空用户信息
    function clearUserInfo() {
      userInfo.value = {} as UserInfo
    }

    return {
      token,
      isFirstInit,
      isCollasped,
      userInfo,
      setToken,
      setIsFirstInit,
      setCollasped,
      setUserInfo,
      clearUserInfo,
    }
  },
  {
    persist: {
      storage: localStorage,
      pick: ['token', 'isCollasped', 'userInfo'],
      key: 'login',
    },
  },
)
